
import { Controller, Post, Body, SetMetadata, Request } from '@nestjs/common';
import { BaseDao } from '@/common/baseDao/base.service';
import { Util } from '@/common/util/util.service';
@SetMetadata('isLogger', true)
@Controller()
export class SystemPermissionController {
    dbName: string;
    constructor(
        private baseDao: BaseDao,
        private util: Util,
    ) {
        this.dbName = 'quick-permissions'
    }


    @Post('/getAll')
    async getAll(@Request() req, @Body() data) {
        let whereJson = {}

        const res = await this.baseDao.selects({
            dbName: this.dbName,
            whereJson,
        })

        let treeProps = {
            id: "permission_id",
            parent_id: "parent_id",
            children: "children"
        };

        return {
            ...res,
            list: res['rows'],
            rows: this.util.buildTree(res['rows'], treeProps)
        }
    }

    // 权限绑定
    @Post('/bindPermission')
    async bindPermission(@Body() data) {
        console.log('data', data.permission)

        const { _id, permission } = data

        return await this.baseDao.updateById({
            dbName: 'quick-roles',
            id: _id,
            dataJson: {
                permission
            }
        })
    }

}